有什么方法可以在VBA中模拟Continue语句? |
您所在的位置:网站首页 › vba goto语句用法 › 有什么方法可以在VBA中模拟Continue语句? |
本问题已经有最佳答案,请猛点这里访问。
由于VBA没有Continue语句(或任何类似性质的语句),因此完成同一件事的另一种方法是什么。 相关讨论 那旧的备用goto语句呢?在不提供Continue语句的语言中执行此操作的最佳方法是将其余代码块简单地包装为if条件。 123456For i=1 to 10 'some code here If I_want_to_finish_this_loop 'do your thing End If Next i这样可以避免使用Goto,而您唯一的成本就是逆转条件。 如果您有多个需要继续的地方,最好的选择是在代码中放置一个继续标签,然后转到它。 12345678For i=1 to 10 'some code here If I_dont_want_to_finish_this_loop Goto Continue End If 'finish the loop :Continue Next i 相关讨论 如果有多个地方想跳到循环的结尾? 多个if语句。 你一定是在开玩笑,哈维先生! 所有其他选项更加复杂。 goto语句并不复杂。 太难听了,丑,等等等等。 您是否更愿意将多个Goto(以及必要的标签)用于嵌套的if语句? drdobbs.com/cpp/what-dijkstra-said-was-harmful-about-got/一种方法是使用封闭的Do ... Loop。 当然,这不能在另一个Do ... Loop中使用,但是我们在大多数编程语言中都遇到了这种情况。 1234567891011121314For i=1 To 10 Do 'Do everything in here and If I_Dont_Want_Finish_This_Loop Then Exit Do End If 'Of course, if I do want to finish it, 'I put more stuff here, and then... Loop While False 'quit after one loop Next i 相关讨论 可以说,应该删除第二个exit do,而应该使用loop until true或loop while false。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |